package cn.tedu.boot21.controller;

import cn.tedu.boot21.entity.Emp;
import cn.tedu.boot21.mapper.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jnlp.ClipboardService;
import java.util.List;

@RestController
public class EmpController {
    //自动装配注解,此注解会结合Spring框架和MyBatis生成一个Mapper接口的实现类并且实现了接口中的所有抽象方法
    //然后实例化了该对象赋值给了mapper变量
    @Autowired(required = false)
    EmpMapper mapper;//如果此处报错,是idea的版本问题,可以在注解中加如上参数required = false

    @RequestMapping("/insert")
    public String insert(Emp emp){
        System.out.println("emp = " + emp);
        mapper.insert(emp);
        return "添加成功!";
    }


    @RequestMapping("/select")
    public String select(){
        List<Emp> list = mapper.select();

        String[] html = {"<table border=1>"};
        html[0] +="<caption>员工列表</caption>";
        html[0] +="<tr><th>id</th><th>名字</th><th>工资</th><th>工作</th><th>操作</th></tr>";
        //遍历集合,并把数据添加到表格中
        list.forEach(s->
                html[0] +="<tr><td>"+s.getId()+"</td><td>"+s.getName()+"</td><td>"+s.getSal()+"</td><td>"+s.getJob()+
                        "</td>"+"<td><a href='/delete?id="+s.getId()+"'>删除</a></td></tr>");
        html[0]+="</table>";

        return html[0];
//        return list.toString();
    }

    @RequestMapping("/delete")
    public String delete(int id){
        mapper.delete(id);
        return "删除成功!<a href='/select'>返回列表页面</a>";
    }

    @RequestMapping("/update")
    public String update(Emp emp){
        mapper.update(emp);
        return "删除成功!<a href='/select'>返回列表页面</a>";
    }





}
